/**
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
 * Licensed under the LGPL or a commercial license.
 * For LGPL see License.txt in the project root for license information.
 * For commercial licenses see https://www.tiny.cloud/
 *
 * Version: 5.8.2 (2021-06-23)
 */
(function () {
  'use strict';
  var Cell = function (initial) {
    var value = initial;
    var get = function () {
      return value;
    };
    var set = function (v) {
      value = v;
    };
    return {
      get: get,
      set: set
    };
  };
  var constant = function (value) {
    return function () {
      return value;
    };
  };
  var never = constant(false);
  var always = constant(true);
  var noop = function () {
  };
  var none = function () {
    return NONE;
  };
  var NONE = function () {
    var eq = function (o) {
      return o.isNone();
    };
    var call = function (thunk) {
      return thunk();
    };
    var id = function (n) {
      return n;
    };
    var me = {
      fold: function (n, _s) {
        return n();
      },
      is: never,
      isSome: never,
      isNone: always,
      getOr: id,
      getOrThunk: call,
      getOrDie: function (msg) {
        throw new Error(msg || 'error: getOrDie called on none.');
      },
      getOrNull: constant(null),
      getOrUndefined: constant(undefined),
      or: id,
      orThunk: call,
      map: none,
      each: noop,
      bind: none,
      exists: never,
      forall: always,
      filter: none,
      equals: eq,
      equals_: eq,
      toArray: function () {
        return [];
      },
      toString: constant('none()')
    };
    return me;
  }();
  var some = function (a) {
    var constant_a = constant(a);
    var self = function () {
      return me;
    };
    var bind = function (f) {
      return f(a);
    };
    var me = {
      fold: function (n, s) {
        return s(a);
      },
      is: function (v) {
        return a === v;
      },
      isSome: always,
      isNone: never,
      getOr: constant_a,
      getOrThunk: constant_a,
      getOrDie: constant_a,
      getOrNull: constant_a,
      getOrUndefined: constant_a,
      or: self,
      orThunk: self,
      map: function (f) {
        return some(f(a));
      },
      each: function (f) {
        f(a);
      },
      bind: bind,
      exists: bind,
      forall: bind,
      filter: function (f) {
        return f(a) ? me : NONE;
      },
      toArray: function () {
        return [a];
      },
      toString: function () {
        return 'some(' + a + ')';
      },
      equals: function (o) {
        return o.is(a);
      },
      equals_: function (o, elementEq) {
        return o.fold(never, function (b) {
          return elementEq(a, b);
        });
      }
    };
    return me;
  };
  var from = function (value) {
    return value === null || value === undefined ? NONE : some(value);
  };
  var Optional = {
    some: some,
    none: none,
    from: from
  };
  var revocable = function (doRevoke) {
    var subject = Cell(Optional.none());
    var revoke = function () {
      return subject.get().each(doRevoke);
    };
    var clear = function () {
      revoke();
      subject.set(Optional.none());
    };
    var isSet = function () {
      return subject.get().isSome();
    };
    var set = function (s) {
      revoke();
      subject.set(Optional.some(s));
    };
    return {
      clear: clear,
      isSet: isSet,
      set: set
    };
  };
  var unbindable = function () {
    return revocable(function (s) {
      return s.unbind();
    });
  };
  var onSetupFormatToggle = function (editor, name) {
    return function (api) {
      var boundCallback = unbindable();
      var init = function () {
        editor.formatter.register("sub", [
          {
            inline: 'sub',
            remove: 'all',
            preserve_attributes: [
              'class',
              'style'
            ]
          }])
        editor.formatter.register("sup", [
          {
            inline: 'sup',
            remove: 'all',
            preserve_attributes: [
              'class',
              'style'
            ]
          }])
        api.setActive(editor.formatter.match(name));
        var binding = editor.formatter.formatChanged(name, api.setActive);
        boundCallback.set(binding);
      };
      editor.initialized ? init() : editor.on('init', init);
      return boundCallback.clear;
    };
  };
  var toggleFormat = function (editor, fmt) {
    return function () {
      editor.execCommand('mceToggleFormat', false, fmt);
    };
  };
  tinymce.PluginManager.add('suborsup', function (editor) {
    var global$c = tinymce.util.Tools.resolve('tinymce.util.Tools');
    global$c.each([
      {
        name: 'sub',
        text: '下标',
        icon: 'icon-xiabiao'
      },
      {
        name: 'sup',
        text: '上标',
        icon: 'icon-shangbiao'
      }], function (btn, _idx) {
        editor.ui.registry.addToggleButton(btn.name, {
          tooltip: btn.text,
          icon: btn.icon,
          onSetup: onSetupFormatToggle(editor, btn.name),
          onAction: toggleFormat(editor, btn.name)
        });
      });
  });

}());
